plotrixで作図

散布図+エラーバー

library(plotrix)
x <- 1:3
y <- c(1, 3, 5)
el <- c(0.5, 0.2, 0.8)  # エラーバー下側幅
eu <- c(0.2, 0.4, 0.3)  # エラーバー上側幅
plotCI(x, y, el, eu)

plot of chunk r04a

棒グラフ+エラーバー

m <- c(10, 15, 20)
names(m) <- c("S", "M", "L")
sd <- c(2, 3, 4)
plotCI(barplot(m, col = "gray", ylim = c(0, max(m + sd))), m, sd, 
    add = TRUE)

plot of chunk r04b

100個の平均の信頼区間

my.ci <- function(x, n) {
    y <- rnorm(n)
    fit <- lm(y ~ 1)
    res <- c(coef(fit), confint(fit, level = 0.95))
    names(res) <- c("mean", "lb95", "ub95")
    return(res)
}
x <- 1:100
res <- t(sapply(x, my.ci, n = 100))
cols <- ifelse(res[, 2] > 0 | res[, 3] < 0, "red", "gray70")
m <- res[, 1]
ue <- res[, 3] - m
le <- m - res[, 2]
plotCI(x, m, ue, le, col = cols)
abline(h = 0)

plot of chunk r04c